package com.dingwen.treasure.code.generate.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dingwen.treasure.base.constant.SysConfigConstant;
import com.dingwen.treasure.base.controller.BaseViewController;
import com.dingwen.treasure.base.enums.DicTypeEnum;
import com.dingwen.treasure.base.enums.StatusEnum;
import com.dingwen.treasure.base.pojo.vo.Result;
import com.dingwen.treasure.code.generate.constant.GenerateConstant;
import com.dingwen.treasure.code.generate.util.FreeMarkerUtil;
import com.dingwen.treasure.model.entity.mybatisplus.SysDictionary;
import com.dingwen.treasure.mybatisplus.service.SysDictionaryService;
import com.dingwen.treasure.mybatisplus.util.SysConfigUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 　GenerateEnumController
 * 　@author dingwen
 * 　@date 2022/7/13
 */
@Api(tags = "枚举生成API")
@RestController
@Slf4j
@RequestMapping("enum")
public class GenerateEnumController implements BaseViewController {

    /**
     * sysDictionaryService
     */
    @Resource
    private SysDictionaryService sysDictionaryService;

    /**
     * sysConfigUtil
     */
    @Resource
    private SysConfigUtil sysConfigUtil;


    /**
     * freeMarkerUtil
     */
    @Resource
    private FreeMarkerUtil freeMarkerUtil;

    @ApiOperation(value = "生成")
    @ApiImplicitParam(value = "模版文件", name = "templatePath")
    @GetMapping("generate/{templatePath}")
    public Result<Void> generate(@PathVariable("templatePath") String templatePath) {
        // 生成文件存储位置
        String targetPath = sysConfigUtil.getValueOrDefault(SysConfigConstant.DIC_GENERATE_LOCATION, File.separator);
        // 获取主枚举
        final LambdaQueryWrapper<SysDictionary> queryWrapper = new LambdaQueryWrapper<>();
        // 逻辑删除mybatis plus 已经自动完成
        queryWrapper.eq(SysDictionary::getDicType, DicTypeEnum.ENUM);
        queryWrapper.eq(SysDictionary::getDicState, StatusEnum.NORMAL);
        queryWrapper.eq(SysDictionary::getParentId, 0);
        final List<SysDictionary> sysDictionariesForFather = sysDictionaryService.list(queryWrapper);
        Assert.notNull(sysDictionariesForFather, "枚举项配置空，未进行代码生成");
        // 获取孩子枚举
        sysDictionariesForFather.parallelStream()
                .forEach(ele -> {
                    final LambdaQueryWrapper<SysDictionary> wrapper = new LambdaQueryWrapper<>();
                    // 逻辑删除mybatis plus 已经自动完成
                    wrapper.eq(SysDictionary::getDicType, DicTypeEnum.ENUM);
                    wrapper.eq(SysDictionary::getDicState, StatusEnum.NORMAL);
                    wrapper.eq(SysDictionary::getParentId, ele.getDicId());
                    final List<SysDictionary> sysDictionariesForChildren = sysDictionaryService.list(wrapper);
                    Assert.notEmpty(sysDictionariesForChildren, "枚举子项不能为空,请您检查配置");
                    Map<String, Object> params = new HashMap<>(2);
                    params.put(GenerateConstant.TEMPLATE_OBJ_NAME, ele);
                    params.put(GenerateConstant.TEMPLATE_LIST_NAME, sysDictionariesForChildren);
                    // 生成枚举文件
                    freeMarkerUtil.generate(params, templatePath,
                            targetPath.concat(ele.getDicName()).concat(GenerateConstant.FILE_SUFFIX));
                });
        return success();
    }
}


